package model

import (
	"time"

	"gorm.io/gorm"
)

type InfoType string // 自定义枚举类型

const (
	TransferType    InfoType = "transfer"
	PurchaseType    InfoType = "purchase"
	RecruitmentType InfoType = "recruitment"
)

type Favorite struct {
	FavoriteID   uint      `gorm:"primaryKey;autoIncrement;column:favorite_id"`                       // 主键自增
	UserID       uint      `gorm:"not null;index:uniq_collect"`                                       // 外键+联合索引
	InfoID       uint      `gorm:"not null;index:uniq_collect"`                                       // 收藏信息ID
	InfoType     InfoType  `gorm:"type:enum('transfer','purchase','recruitment');index:uniq_collect"` // 信息类型枚举
	IsDeleted    bool      `gorm:"column:is_deleted;default:false;comment:是否已软删除"`
	FavoriteTime time.Time `gorm:"autoCreateTime"`                                                    // 自动填充收藏时间
}

// 自定义表名（与数据库表名一致）
func (Favorite) TableName() string {
	return "favorites"
}

// 外键关联（需确保 User 模型存在）
func (f *Favorite) User(db *gorm.DB) (*User, error) {
	var user User
	if err := db.Model(&User{}).Where("user_id = ?", f.UserID).First(&user).Error; err != nil {
		return nil, err
	}
	return &user, nil
}
